// dataFormatter.js
import { ApiData, FormattedItem } from '@/types/dashboard/updateLog'; // 假设类型定义文件位置
import dashboard from '@/modules';
// getApiConfig选项卡api，TABLECONFIG：表格配置，WORKERSTATUS：工单处理回执状态
const { WORKERSTATUS, WORKERTYPE } = dashboard;
// 字段映射
const tabFieldMapping: Record<number, (item: ApiData) => FormattedItem> = {
  // 1-系统更新 2-工单通知 3-版本更新
  1: (item: ApiData) => ({
    id: item.id,
    type: item.text || '-',
    description: item.name || '-',
    date: item.create_time || '-',
  }),
  2: (item: ApiData) => ({
    id: item.id!,
    // 类型
    type: `【${WORKERTYPE[item.type]}】`,
    // 时间
    date: item.create_time,
    // 状态
    status: WORKERSTATUS[item.status],
    // 不同状态不同class样式
    statusClass: 'workorder-status-' + item.status,
    // 描述
    description: `工单：${item.orderNo},${WORKERSTATUS[item.status]}`,
  }),
  3: (item: ApiData) => ({
    id: item.id,
    type: item.text || '-',
    description: `【${item.text}】` + item.name || '-',
    date: item.create_time || '-',
  }),
};

// 获取字段映射器
export const getMapper = (
  tabId: number
): ((item: ApiData) => FormattedItem) => {
  return (
    tabFieldMapping[tabId] ||
    (() => ({
      id: '',
      type: '',
      date: '',
      status: '',
      statusClass: '',
      description: '',
      title: '',
      name: '',
    }))
  );
};

// 用于工单字段配置
export const WORKERFIELDS = [
  { key: 'status', label: '状态', format: (value: any) => WORKERSTATUS[value] },
  {
    key: 'type',
    label: '工单类型',
    format: (value: any) => WORKERTYPE[value],
  },
  { key: 'orderNo', label: '工单编号' },
  { key: 'creationName', label: '用户账号' },
  {
    key: 'orderStudent',
    label: '申请人',
    format: (value: string) => (value ? value.split('\\n')[0] : ''), // 空值返回空字符串
  },
  { key: 'create_time', label: '申请时间' },
  { key: 'do_time', label: '处理时间' },
  { key: 'content', label: '申请内容' },
  { key: 'remark', label: '申请原因' },
  { key: 'auditRemark', label: '处理回执' },
];
